package com.ls.lserp.modules.material.web;

import com.ls.lserp.common.config.Global;
import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.common.web.BaseController;
import com.ls.lserp.modules.material.entity.InventoryClothesDetail;
import com.ls.lserp.modules.material.entity.SlopworkStock;
import com.ls.lserp.modules.material.service.SlopworkStockService;
import com.ls.lserp.modules.sys.entity.Dict;
import com.ls.lserp.modules.sys.utils.DictUtils;
import freemarker.template.Configuration;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhouyang on 2017/12/6.
 */
@Controller
@RequestMapping(value = "${adminPath}/slopwork/stock/")
public class SlopworkStockController extends BaseController {
    // 保存 编辑 删除 方法
    @Autowired
    private SlopworkStockService slopworkStockService;
    @Autowired
    private Configuration freemarkerConfiguration;

    /**
     * 成衣库存一览画面
     *
     *
     * @return
     */
    @RequestMapping(value = { "" })
    public String slopworklStock(Model model) {
        // 款式类别
        List<Dict> styleList2 = DictUtils.getDictList(ResourcesUtil.getConfig("style_category"));
        model.addAttribute("styleList", styleList2);
        return "modules/material/slopworkStock";
    }

    /**
     * @param slopworkStock
     * @param request
     * @param response
     * @return searchList
     * @throws Exception
     * @Title: 画面检索
     * @Description: 查询页面
     */
    @RequiresPermissions("slopwork:stock:view")
    @RequestMapping(value = {"searchList"})
    @ResponseBody
    public String searchList(SlopworkStock slopworkStock ,HttpServletRequest request, HttpServletResponse response,
                             Model model, String[] values) throws Exception {
        Map<Object, Object> listMap = new HashMap<Object, Object>();
        request.setAttribute(Global.LIST_NAME, "slopworkStock");
        // 画面取得的检索项目
        // 款号
        String styleNo = slopworkStock.getStyleNo();
        // 客户
        String ognizationName = slopworkStock.getOgnizationName();
        // 款式类别
        String styleCateGoryCd = slopworkStock.getStyleCateGoryCd();
        // 日期
        String dateFrom = slopworkStock.getDateFrom();
        // 日期
        String dateTo = slopworkStock.getDateTo();
        // 拼接字符串供分页使用
        StringBuilder searchParam = new StringBuilder();
        searchParam.append("&styleNo=");
        searchParam.append(styleNo);
        searchParam.append("&ognizationName=");
        searchParam.append(ognizationName);
        searchParam.append("&styleCateGoryCd=");
        searchParam.append(styleCateGoryCd);
        searchParam.append("&dateFrom=");
        searchParam.append(dateFrom);
        searchParam.append("&dateTo=");
        searchParam.append(dateTo);
        // 放到Attribute里，使用Global类里面的SEARCH_PAEAM作为key
        request.setAttribute(Global.SEARCH_PAEAM, searchParam.toString());
        Page<SlopworkStock> page = slopworkStockService.findList(new Page<SlopworkStock>(request,response),slopworkStock);
        String strHtml = "";
        String strUri = request.getContextPath() + Global.getAdminPath();
        Map<String, Object> map = new HashedMap();
        map.put("adminUri", strUri);
        map.put("users", page.getList());
        map.put("pageNo", page.getPageNo());
        map.put("pageSize", page.getPageSize());
        // 查询一览页面数据
        strHtml = FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfiguration.getTemplate("slopworkStock.ftl"), map);
        listMap.put("slopworkStock", strHtml);
        listMap.put("page", page.getHtml());
        // ajax返回结果
        return renderString(response, listMap);

    }
    /**
     * @param inventoryClothesDetail
     * @return searchList
     * @throws Exception
     * @Title: 子项查询
     * @Description: 查询页面
     */
    @RequiresPermissions("slopwork:stock:view")
    @RequestMapping(value = {"detailShow"})
   public String detailShow(InventoryClothesDetail inventoryClothesDetail, HttpServletRequest request, HttpServletResponse response,
                            Model model){
        inventoryClothesDetail =slopworkStockService.findInOutDetail(inventoryClothesDetail);
        model.addAttribute("inventoryClothesDetail",inventoryClothesDetail);
        return "modules/material/slopworkStockDetailShow";
    }
}
